package com.csair.seam.controller.pc;


import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.csair.seam.domain.model.entity.AssetsCategoryStatistics;
import com.csair.seam.domain.model.entity.AssetsStatisticsOverview;
import com.csair.seam.domain.model.entity.FaAssetDepreciation;
import com.csair.seam.domain.model.entity.FaEquip;
import com.csair.seam.domain.model.entity.FaEquipRatio;
import com.csair.seam.domain.model.entity.MarketAccounting;
import com.csair.seam.domain.model.entity.VehicleLossStatistics;
import com.csair.seam.domain.model.enums.VisitsModule;
import com.csair.seam.domain.model.param.EquipParam;
import com.csair.seam.domain.model.param.FaAssetQueryParam;
import com.csair.seam.domain.model.param.FaEquipParam;
import com.csair.seam.domain.model.param.MarketAccountingQueryParam;
import com.csair.seam.domain.model.vo.AssetsChangeExportVo;
import com.csair.seam.domain.model.vo.AssetsChangeVo;
import com.csair.seam.domain.model.vo.EquipStatisticsOverviewVo;
import com.csair.seam.domain.model.vo.NewAddAndScrapVo;
import com.csair.seam.domain.model.vo.VehicleLossStatisticsVo;
import com.csair.seam.domain.model.vo.VehicleSituationVo;
import com.csair.seam.domain.service.IAssetsCategoryStatisticsService;
import com.csair.seam.domain.service.IAssetsChangeService;
import com.csair.seam.domain.service.IAssetsStatisticsOverviewService;
import com.csair.seam.domain.service.IFaAssetService;
import com.csair.seam.domain.service.IFaFinanceService;
import com.csair.seam.domain.service.IMarketAccountingService;
import com.csair.seam.domain.service.INewAddAndScrapService;
import com.csair.seam.domain.service.IVehicleLossStatisticsService;
import com.csair.seam.domain.service.IVehicleSituationService;
import com.csair.seam.infrastructure.log.LogOperation;
import com.csair.seam.infrastructure.log.VisitsLog;
import com.csair.seam.infrastructure.poi.utils.ExcelPoiUtil;
import com.csair.seam.infrastructure.utils.DateHelper;
import com.csair.seam.infrastructure.web.BaseController;
import com.csair.seam.infrastructure.web.BaseResponse;
import com.csair.seam.infrastructure.web.ResponseCode;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @description 效能统计分析Controller
 * @author lanrunli
 *
 */
@Api(tags = "效能统计分析")
@RestController
@RequestMapping("/pc/statistics")
public class StatisticsController extends BaseController {

    @Autowired
    private IAssetsStatisticsOverviewService assetsStatisticsOverviewService;
    
    @Autowired
    private IVehicleLossStatisticsService vehicleLossStatisticsService;
    
    @Autowired
    private IAssetsCategoryStatisticsService assetsCategoryStatisticsService;
    
    @Autowired
    private IVehicleSituationService vehicleSituationService;
    
    @Autowired
    private INewAddAndScrapService newAddAndScrapService;
    
    @Autowired
    private IAssetsChangeService assetsChangeService;
    
    @Autowired
    private IFaAssetService faAssetService;
    
    @Autowired
    private IMarketAccountingService marketAccountingService;
    
    @Autowired
    private IFaFinanceService faFinanceService;
    
    @GetMapping("/assets/overview")
    @ApiOperation(value = "资产统计概览", notes = "getEquipOverViewStatistics",httpMethod="GET")
    @LogOperation(bizName = "查询资产统计概览")
//    @VisitsLog(module = VisitsModule.ASSET_STATISTICS_OVERVIEW)
    @VisitsLog(module = VisitsModule.ASSET_STATISTICS_OVERVIEW_QUERY)
    public BaseResponse<EquipStatisticsOverviewVo> getEquipOverViewStatistics(){
    	EquipStatisticsOverviewVo vo = new EquipStatisticsOverviewVo();
    	vo.setCompanyEquipStatistics(assetsStatisticsOverviewService.findCompanyEquipStatistics());
    	vo.setOrgEquipStatistics(assetsStatisticsOverviewService.findOrgEquipStatistics());
        return  buildSuccess(vo);
    }
    
    @GetMapping("/assets/overview/export")
    @ApiOperation(value = "资产统计概览-导出", notes = "exportEquipOverViewStatistics",httpMethod="GET")
    @LogOperation(bizName = "导出资产统计概览")
    @VisitsLog(module = VisitsModule.ASSET_STATISTICS_OVERVIEW_EXPORT)
    public BaseResponse<Object> exportEquipOverViewStatistics(HttpServletRequest request,HttpServletResponse response){
    	List<AssetsStatisticsOverview> data = assetsStatisticsOverviewService.findOrgEquipStatistics();
    	if(CollectionUtils.isEmpty(data)) {
    		return new BaseResponse<>(ResponseCode.ERROR, "查询的结果集为空",null);
    	}
    	ExcelPoiUtil.exportExcel("资产统计概览.xlsx", data, null, request, response);
        return  buildSuccess(null);
    }
    
    @GetMapping("/assets/category")
    @ApiOperation(value = "资产分类统计 ", notes = "getAssetsCategoryStatistics",httpMethod="GET")
    @LogOperation(bizName = "查询资产分类统计")
//    @VisitsLog(module = VisitsModule.ASSET_CATEGORY_STATISTICS)
    @VisitsLog(module = VisitsModule.ASSET_CATEGORY_STATISTICS_QUERY)
    public BaseResponse<List<AssetsCategoryStatistics>> getAssetsCategoryStatistics(
    		@RequestParam(value = "categoryId", required = false) @ApiParam(value = "设备类别", required = false) String categoryId,
            @RequestParam(value = "code", required = false) @ApiParam(value = "所属单位", required = false) String orgCode) {
    	return buildSuccess(assetsCategoryStatisticsService.queryEquipCategoryCount(categoryId, orgCode));
    }
    
    @GetMapping("/assets/category/export")
    @ApiOperation(value = "资产分类统计-导出 ", notes = "exportAssetsCategoryStatistics",httpMethod="GET")
    @LogOperation(bizName = "导出资产分类统计")
    @VisitsLog(module = VisitsModule.ASSET_CATEGORY_STATISTICS_EXPORT)
    public BaseResponse<Object> exportAssetsCategoryStatistics(
    		@RequestParam(value = "categoryId", required = false) @ApiParam(value = "设备类别", required = false) String categoryId,
            @RequestParam(value = "code", required = false) @ApiParam(value = "所属单位", required = false) String orgCode,
            HttpServletRequest request,HttpServletResponse response) {
    	List<AssetsCategoryStatistics> data = assetsCategoryStatisticsService.queryEquipCategoryCount(categoryId, orgCode);
    	if(CollectionUtils.isEmpty(data)) {
    		return new BaseResponse<>(ResponseCode.ERROR, "查询的结果集为空",null);
    	}
    	ExcelPoiUtil.exportExcel("资产分类统计.xlsx", data, null, request, response);
    	return buildSuccess(null);
    }
    
    @GetMapping("/vehicle/situation")
    @ApiOperation(value = "车辆总体情况 ", notes = "getVehicleSituationStatistics",httpMethod="GET")
    @LogOperation(bizName = "查询车辆总体情况")
//    @VisitsLog(module = VisitsModule.VEHICLE_SITUATION_STATISTICS)
    @VisitsLog(module = VisitsModule.VEHICLE_SITUATION_STATISTICS_QUERY)
    public BaseResponse<Collection<VehicleSituationVo>> getVehicleSituationStatistics() {
    	return buildSuccess(vehicleSituationService.findVehicleSituation());
    }
    
    @GetMapping("/vehicle/situation/export")
    @ApiOperation(value = "车辆总体情况 -导出", notes = "exportVehicleSituationStatistics",httpMethod="GET")
    @LogOperation(bizName = "导出车辆总体情况")
    @VisitsLog(module = VisitsModule.VEHICLE_SITUATION_STATISTICS_EXPORT)
    public BaseResponse<Object> exportVehicleSituationStatistics(HttpServletRequest request,HttpServletResponse response) {
    	List<VehicleSituationVo> data = vehicleSituationService.findVehicleSituation();
    	if(CollectionUtils.isEmpty(data)) {
    		return new BaseResponse<>(ResponseCode.ERROR, "查询的结果集为空",null);
    	}
    	ExcelPoiUtil.exportExcel("车辆总体情况.xlsx", data, null, request, response);
    	return buildSuccess(null);
    }
    
    @GetMapping("/vehicle/loss")
    @ApiOperation(value = "车辆损耗统计 ", notes = "getVehicleLossStatistics",httpMethod="GET")
    @LogOperation(bizName = "查询车辆损耗统计")
//    @VisitsLog(module = VisitsModule.VEHICLE_LOSS_STATISTICS)
    @VisitsLog(module = VisitsModule.VEHICLE_LOSS_STATISTICS_QUERY)
    public BaseResponse<VehicleLossStatisticsVo> getVehicleLossStatistics() {
    	return buildSuccess(vehicleLossStatisticsService.findVehicleLossData());
    }
    
    @GetMapping("/vehicle/loss/general/export")
    @ApiOperation(value = "车辆损耗统计-通用车辆损耗导出 ", notes = "exportGeneralVehicleLossStatistics",httpMethod="GET")
    @LogOperation(bizName = "通用车辆损耗导出")
    @VisitsLog(module = VisitsModule.VEHICLE_LOSS_STATISTICS_GENERAL_EXPORT)
    public BaseResponse<Object> exportGeneralVehicleLossStatistics(HttpServletRequest request,HttpServletResponse response) {
    	List<VehicleLossStatistics> data = vehicleLossStatisticsService.getGeneralVehicleLoss();
    	if(CollectionUtils.isEmpty(data)) {
    		return new BaseResponse<>(ResponseCode.ERROR, "查询的结果集为空",null);
    	}
    	ExcelPoiUtil.exportExcel("通用车辆损耗统计.xlsx", data, null, request, response);
    	return buildSuccess(null);
    }
    
    @GetMapping("/vehicle/loss/special/export")
    @ApiOperation(value = "车辆损耗统计-特种车辆损耗导出 ", notes = "exportSpecialVehicleLossStatistics",httpMethod="GET")
    @LogOperation(bizName = "特种车辆损耗导出")
    @VisitsLog(module = VisitsModule.VEHICLE_LOSS_STATISTICS_SPECIAL_EXPORT)
    public BaseResponse<Object> exportSpecialVehicleLossStatistics(HttpServletRequest request,HttpServletResponse response) {
    	List<VehicleLossStatistics> data = vehicleLossStatisticsService.getSpecialVehicleLoss();
    	if(CollectionUtils.isEmpty(data)) {
    		return new BaseResponse<>(ResponseCode.ERROR, "查询的结果集为空",null);
    	}
    	ExcelPoiUtil.exportExcel("特种车辆损耗统计.xlsx", data, null, request, response);
    	return buildSuccess(null);
    }
    
    @GetMapping("/new/scrap")
    @ApiOperation(value = "新增报废统计 ", notes = "getNewAddAndScrap",httpMethod="GET")
    @LogOperation(bizName = "查询新增报废统计")
//    @VisitsLog(module = VisitsModule.ADD_SCRAP_STATISTICS)
    @VisitsLog(module = VisitsModule.ADD_SCRAP_STATISTICS_QUERY)
    public BaseResponse<List<NewAddAndScrapVo>> getNewAddAndScrap(@ApiParam(value = "查询参数")EquipParam param) {
    	checkDateParam(param);
    	return buildSuccess(newAddAndScrapService.findNewAddAndScrap(param));
    }
    
    @GetMapping("/new/scrap/export")
    @ApiOperation(value = "新增报废统计-导出 ", notes = "exprtNewAddAndScrap",httpMethod="GET")
    @LogOperation(bizName = "导出新增报废统计")
    @VisitsLog(module = VisitsModule.ADD_SCRAP_STATISTICS_EXPORT)
    public BaseResponse<Object> exprtNewAddAndScrap(@ApiParam(value = "查询参数")EquipParam param,
    		HttpServletRequest request,HttpServletResponse response) {
    	checkDateParam(param);
    	List<NewAddAndScrapVo> data = newAddAndScrapService.findNewAddAndScrap(param);
    	if(CollectionUtils.isEmpty(data)) {
    		return new BaseResponse<>(ResponseCode.ERROR, "查询的结果集为空",null);
    	}
    	ExcelPoiUtil.exportExcel("新增报废统计.xlsx", data, null, request, response);
    	return buildSuccess(null);
    }
    
    @GetMapping("/assets/change")
    @ApiOperation(value = "资产变动统计 ", notes = "getAssetsChange",httpMethod="GET")
    @LogOperation(bizName = "查询资产变动统计")
//    @VisitsLog(module = VisitsModule.ASSET_CHANGE_STATISTICS)
    @VisitsLog(module = VisitsModule.ASSET_CHANGE_STATISTICS_QUERY)
    public BaseResponse<List<AssetsChangeVo>> getAssetsChange(@ApiParam(value = "查询参数")EquipParam param) {
    	checkDateParam(param);
    	return buildSuccess(assetsChangeService.findAssetsChange(param));
    }
    
    @GetMapping("/assets/change/export")
    @ApiOperation(value = "资产变动统计-导出 ", notes = "exportAssetsChange",httpMethod="GET")
    @LogOperation(bizName = "导出资产变动统计")
    @VisitsLog(module = VisitsModule.ASSET_CHANGE_STATISTICS_EXPORT)
    public BaseResponse<Object> exportAssetsChange(@ApiParam(value = "查询参数")EquipParam param,
    		HttpServletRequest request,HttpServletResponse response) {
    	checkDateParam(param);
    	List<AssetsChangeVo> list = assetsChangeService.findAssetsChange(param);
    	if(CollectionUtils.isEmpty(list)) {
    		return new BaseResponse<>(ResponseCode.ERROR, "查询的结果集为空",null);
    	}
    	List<AssetsChangeExportVo> data = new ArrayList<>();
    	NumberFormat numberFormat = NumberFormat.getInstance();
		numberFormat.setMaximumFractionDigits(1);
    	for (AssetsChangeVo assetsChangeVo : list) {
    		AssetsChangeExportVo vo = new AssetsChangeExportVo();
    		vo.setOrgName(assetsChangeVo.getOrgName());
    		vo.setLocationChange(assetsChangeVo.getLocationChangeUnfinishedNum()
    				+"/"+assetsChangeVo.getLocationChangeFinishedNum()
    				+"/"+numberFormat.format(assetsChangeVo.getLocationChangeFinishedRate())+"%");
    		vo.setUserChange(assetsChangeVo.getUserChangeUnfinishedNum()
    				+"/"+assetsChangeVo.getUserChangeFinishedNum()
    				+"/"+numberFormat.format(assetsChangeVo.getUserChangeFinishedRate())+"%");
    		vo.setUseDeptChange(assetsChangeVo.getUseDeptChangeUnfinishedNum()
    				+"/"+assetsChangeVo.getUseDeptChangeFinishedNum()
    				+"/"+numberFormat.format(assetsChangeVo.getUseDeptChangeFinishedRate())+"%");
    		vo.setReduce(assetsChangeVo.getReduceUnfinishedNum()
    				+"/"+assetsChangeVo.getReduceFinishedNum()
    				+"/"+numberFormat.format(assetsChangeVo.getReduceFinishedRate())+"%");
    		vo.setReDeployOut(assetsChangeVo.getReDeployOutUnfinishedNum()
    				+"/"+assetsChangeVo.getReDeployOutFinishedNum()
    				+"/"+numberFormat.format(assetsChangeVo.getReDeployOutFinishedRate())+"%");
    		vo.setAvgFinishedRate(assetsChangeVo.getAvgFinishedRate()+"%");
    		data.add(vo);
		}
    	ExcelPoiUtil.exportExcel("资产变动统计.xlsx", data, null, request, response);
    	return buildSuccess(null);
    }
    
    private void checkDateParam(EquipParam param) {
    	if(StringUtils.isBlank(param.getStartDate())) {
    		Date date = new Date();
    		date = DateUtils.setMonths(date, 0);
    		date = DateUtils.setDays(date, 1);
    		param.setStartDate(DateHelper.formatDateStr(date, DateHelper.PATTERN_DATE));
    	}
    	if(StringUtils.isBlank(param.getEndDate())) {
    		param.setEndDate(DateHelper.getCurrentDateStr());
    	}
    }
    
    @GetMapping("/faasset/depreciation")
    @ApiOperation(value = "固定资产折旧数据报表统计 ", notes = "getFaAssetDepreciation",httpMethod="GET")
    @LogOperation(bizName = "固定资产折旧数据报表统计")
//    @VisitsLog(module = VisitsModule.FA_ASSET_DEPRECIATION)
    @VisitsLog(module = VisitsModule.FA_ASSET_DEPRECIATION_QUERY)
    public BaseResponse<IPage<FaAssetDepreciation>> getFaAssetDepreciation(@ApiParam(value = "查询参数")FaAssetQueryParam param) {
    	if(StringUtils.isBlank(param.getMonth())) {
    		return new BaseResponse<>(ResponseCode.ERROR, "月份为必选条件",null);
    	}
    	return buildSuccess(faAssetService.queryFaAssetDepreciationPage(param));
    }
    
    @GetMapping("/faasset/depreciation/export")
    @ApiOperation(value = "固定资产折旧数据报表统计-导出 ", notes = "exportFaAssetDepreciation",httpMethod="GET")
    @LogOperation(bizName = "导出固定资产折旧数据报表统计")
    @VisitsLog(module = VisitsModule.FA_ASSET_DEPRECIATION_EXPORT)
    public BaseResponse<Object> exportFaAssetDepreciation(@ApiParam(value = "查询参数")FaAssetQueryParam param,
    		HttpServletRequest request,HttpServletResponse response){
    	if(StringUtils.isBlank(param.getMonth())) {
    		return new BaseResponse<>(ResponseCode.ERROR, "月份为必填条件",null);
    	}
    	List<FaAssetDepreciation> data = faAssetService.queryFaAssetDepreciation(param);
    	if(CollectionUtils.isEmpty(data)) {
    		return new BaseResponse<>(ResponseCode.ERROR, "查询的结果集为空",null);
    	}
    	String[] arrs = param.getMonth().split("-");
    	ExcelPoiUtil.exportExcel(arrs[1]+"年"+arrs[0]+"月固定资产设备折旧报表数据.xlsx", data, null, request, response);
    	return buildSuccess(null);
    }
    
    @GetMapping("/market/accounting")
    @ApiOperation(value = "市场化核算统计 ", notes = "getMarketAccounting",httpMethod="GET")
    @LogOperation(bizName = "市场化核算统计")
//    @VisitsLog(module = VisitsModule.MARKET_ACCOUNTING)
    @VisitsLog(module = VisitsModule.MARKET_ACCOUNTING_QUERY)
    public BaseResponse<List<MarketAccounting>> getMarketAccounting(@ApiParam(value = "查询参数")MarketAccountingQueryParam param) {
    	if(StringUtils.isBlank(param.getMonth())){
    		return new BaseResponse<>(ResponseCode.ERROR, "月份为必填条件",null);
    	}
    	return buildSuccess(marketAccountingService.queryMarketAccounting(param));
    }
    
    @GetMapping("/market/accounting/export")
    @ApiOperation(value = "市场化核算统计-导出 ", notes = "exportMarketAccounting",httpMethod="GET")
    @LogOperation(bizName = "导出市场化核算统计")
    @VisitsLog(module = VisitsModule.MARKET_ACCOUNTING_EXPORT)
    public BaseResponse<Object> exportMarketAccounting(@ApiParam(value = "查询参数")MarketAccountingQueryParam param,
            HttpServletRequest request,HttpServletResponse response) {
    	if(StringUtils.isBlank(param.getMonth())){
    		return new BaseResponse<>(ResponseCode.ERROR, "月份为必填条件",null);
    	}
    	List<MarketAccounting> data = marketAccountingService.queryMarketAccounting(param);
    	if(CollectionUtils.isEmpty(data)) {
    		return new BaseResponse<>(ResponseCode.ERROR, "查询的结果集为空",null);
    	}
    	ExcelPoiUtil.exportExcel("市场化核算统计.xlsx", data, null, request, response);
    	return buildSuccess(null);
    }
    
    @GetMapping("/faequip/ratio")
    @ApiOperation(value = "固资设备转固监测报表统计 ", notes = "getFaEquipRatio",httpMethod="GET")
    @LogOperation(bizName = "固资设备转固监测报表统计")
//    @VisitsLog(module = VisitsModule.FA_EQUIP_MONITOR)
    @VisitsLog(module = VisitsModule.FA_EQUIP_MONITOR_QUERY)
    public BaseResponse<FaEquipRatio> getFaEquipRatio(@ApiParam(value = "查询参数")FaEquipParam param) {
    	return buildSuccess(faFinanceService.findFaEquipRatio(param));
    }
    
    @GetMapping("/faequip/detail")
    @ApiOperation(value = "固资设备转固监测报表明细 ", notes = "getFaEquipPage",httpMethod="GET")
    @LogOperation(bizName = "固资设备转固监测报表明细")
    public BaseResponse<IPage<FaEquip>> getFaEquipPage(@ApiParam(value = "查询参数")FaEquipParam param) {
    	return buildSuccess(faFinanceService.findFaEquipPage(param));
    }
    
    @GetMapping("/faequip/export")
    @ApiOperation(value = "固资设备转固监测报表明细-导出 ", notes = "exportFaequip",httpMethod="GET")
    @LogOperation(bizName = "固资设备转固监测报表明细导出")
    @VisitsLog(module = VisitsModule.FA_EQUIP_MONITOR_EXPORT)
    public BaseResponse<Object> exportFaequip(@ApiParam(value = "查询参数")FaEquipParam param,
            HttpServletRequest request,HttpServletResponse response) {
    	List<FaEquip> data = faFinanceService.findFaEquipList(param);
    	if(CollectionUtils.isEmpty(data)) {
    		return new BaseResponse<>(ResponseCode.ERROR, "查询的结果集为空",null);
    	}
    	ExcelPoiUtil.exportExcel("固资设备转固监测报表明细.xlsx", data, null, request, response);
    	return buildSuccess(null);
    }
    
}

